package com.blackhorse.anlis.anli.serviceImpl;

import com.blackhorse.dao.AccountDao;
import com.blackhorse.domain.Account;
import com.blackhorse.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("AccountServiceImplAnnoAnli")
public class AccountServiceImpl implements AccountService {

    @Autowired
    @Qualifier("AccountDaoImplAnnoAnli")
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(int id) {
        return accountDao.findAccountById(id);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(int id) {
        accountDao.deleteAccount(id);
    }

    @Override
    public void transter(String srcName, String tarName, float money) {
        Account srcAccount = accountDao.findAccountByName(srcName);
        Account tarAccount = accountDao.findAccountByName(tarName);

        srcAccount.setMoney(srcAccount.getMoney() - money);
        tarAccount.setMoney(tarAccount.getMoney() + money);

        accountDao.updateAccount(srcAccount);
        //如果这里出现异常，事务回滚
        int a = 1 / 0;
        accountDao.updateAccount(tarAccount);
    }
}
